package top.hidoctor.auscultation.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;

import com.tang.util.BigDecimalUtil;
import com.tang.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import top.hidoctor.auscultation.base.Constant;

public class EcgView extends View {
    private final Context context;
    //折线的画笔
    private Paint mPaint_brokenline;
    //两点间横坐标间距
    private float gap_x = 0;
    private List<Float> mData = new ArrayList<>();
    private int width;
    private int height;
    //路径
    private Path mpath = new Path();

    public void setMdata(List<Float> list, float gap_x) {
        this.gap_x = gap_x;
        // -0.55 - 0.55 转 0-110
        // -1 到 1 之间数据  转变 0-200
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) != null) {
                mData.add((float) (list.get(i) + 0.55) * 100);
            }
        }


        requestLayout();//相当于调用onMeasure方法
        invalidate();//相当于调用onDraw方法
    }


    public EcgView(Context context) {
        super(context);
        this.context = context;

        init();
    }

    public EcgView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {

        //折线的画笔
        mPaint_brokenline = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint_brokenline.setColor(Color.parseColor("#0F88F2"));
        mPaint_brokenline.setTextSize(18);
        mPaint_brokenline.setTextAlign(Paint.Align.CENTER);
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);


        width = (int) BigDecimalUtil.mul(gap_x, mData.size());
        height = DensityUtil.dip2px(context, 120);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (int i = 0; i < mData.size(); i++) {
            if (i == 0) {
                mpath.moveTo(0,
                        height - height * mData.get(i) / 110);
            }
            if (i != mData.size() - 1) {
                //根据圆点位置绘制折线
                canvas.drawLine(
                        gap_x * i,
                        (float) (height - height * mData.get(i) / 110),
                        gap_x * (i + 1),
                        (float) (height - height * mData.get(i + 1) / 110),
                        mPaint_brokenline);
            }

            if (i == mData.size() - 1) {
//                mpath.quadTo(gridspace_width * i + 10, height - height * mData.get(i), gridspace_width * i + 10, height);
//                mpath.quadTo(gridspace_width * i + 10, height, 10, height);
//                mpath.close();
            }
        }

    }
}
